package com.midian.action.mc.common;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.json.annotations.JSON;

import com.midian.common.dao.OrderHelper;
import com.midian.common.exception.BizException;
import com.midian.common.exception.BizException.Type;
import com.midian.common.pager.Pager;
import com.midian.common.system.SysStatic;
import com.midian.model.common.vo.InfoFlexVo;
import com.midian.model.common.vo.report.ChartVo;
import com.midian.model.user.vo.UserInfoVo;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * @Title:公告操作控制器  
 * @Desription:公告操作控制器
 * @Company:MDKG
 * @ClassName:BaseAction.java
 * @Author:Mengfh
 * @CreateDate:2013-6-7 下午5:38:47  
 * @UpdateUser:Mengfh  
 * @Version:0.1
 */
public class BaseAction extends ActionSupport {
	
	private static final long serialVersionUID = -5319368218954810203L;
	/**登录页面跳转*/
	public static final String LOGIN = "login";
    /**显示导航栏*/
	public static final String INDEX = "index";
	/**无刷新结果*/
	public static final String SUCCESS = "success";
	/**显示列表result*/
	public static final String SHOWLIST = "showList";
	/**显示详细信息result*/
	public static final String SHOWINFO = "showInfo";
	/**保存result*/
	public static final String SAVEINFO = "saveInfo";
	/**修改密码*/
	public static final String CHANGEPWD = "changePwd";
	/**打印result*/
	public static final String PRINT = "print";
	/**显示左右页面的信息result*/
	public static final String LRINFO = "lrInfo";
	/**显示右侧详细信息result*/
	public static final String RIGHT_DETAIL_INFO = "rightDetailInfo";
	/**下载公共结果*/
	public static final String DOWN = "down";
	
	/**响应状态，1为正常响应，0为异常响应*/
	protected int resultCode = SysStatic.REQUEST_CODE_OK;
	/**响应错误的提示信息*/
	protected String errorMessage;
	/**批量操作对应的标识集合*/
	protected String[] ids;
	/**分页组件信息*/
	protected Pager pager;
	/**标识信息*/
	protected String id;
	/**排序属性名称*/
	protected String sortname;
	/**排序规则*/
	protected String sortorder;
	/**批量更新时传入的库存，排序号等信息*/
	protected int[] nums;
	/**价格数组*/
	protected double[] prices;
	
	protected Map<Object, String> statusMap;
	protected String updateURL;
	protected String updateField;
	protected List<String> codeList;
	protected Map<String,Integer> codeMap = new HashMap<String, Integer>();
	private String title;
	/**页面模块路径*/
	protected String modulePath;
	
	/**排序属性抓取*/
	protected boolean isFetch;
	
	/**导出Excel状态*/
	private boolean exportExcelStatus;
	/**导出Excel路径*/
	private String excelFilePath;
	/**Excel目标文件*/
	private File targetFile;
	/**导出Excel文件流*/
	private InputStream inputFile;
	/**导出文件名*/
	private String downloadFileName;

	/**属性名称规则,true为自动添加前缀， false 为原样输出*/
	private boolean auto = true;
	
	/**信息窗口内嵌flexigrid的VO列表*/
	private List<InfoFlexVo> infoFlexVoList;
	
	/**报表VO对象*/
	private ChartVo chartVo;
	
	public ChartVo getChartVo() {
		return chartVo;
	}
	
	public void setChartVo(ChartVo chartVo) {
		this.chartVo = chartVo;
	}
	
	public List<InfoFlexVo> getInfoFlexVoList() {
		return infoFlexVoList;
	}

	public void setInfoFlexVoList(List<InfoFlexVo> infoFlexVoList) {
		this.infoFlexVoList = infoFlexVoList;
	}
	
	public boolean isExportExcelStatus() {
		return exportExcelStatus;
	}

	public void setExportExcelStatus(boolean exportExcelStatus) {
		this.exportExcelStatus = exportExcelStatus;
	}

	public String getExcelFilePath() {
		return excelFilePath;
	}

	public void setExcelFilePath(String excelFilePath) {
		this.excelFilePath = excelFilePath;
	}

	public File getTargetFile() {
		return targetFile;
	}

	public void setTargetFile(File targetFile) {
		this.targetFile = targetFile;
	}

	public InputStream getInputFile() {
		return inputFile;
	}

	public void setInputFile(InputStream inputFile) {
		this.inputFile = inputFile;
	}

	public String getDownloadFileName() {
		return downloadFileName;
	}

	public void setDownloadFileName(String downloadFileName) {
		this.downloadFileName = downloadFileName;
	}

	public BaseAction() {

	}

	public boolean isFetch() {
		return isFetch;
	}

	public void setFetch(boolean isFetch) {
		this.isFetch = isFetch;
	}

	public String getModulePath() {
		return modulePath;
	}

	public void setModulePath(String modulePath) {
		this.modulePath = modulePath;
	}

	public int getResultCode() {
		return resultCode;
	}

	public String getErrorMessage() {
		return errorMessage;
	}
	
	public String[] getIds() {
		return ids;
	}

	public void setIds(String[] ids) {
		this.ids = ids;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Pager getPager() {
		return pager;
	}
	public void setPager(Pager pager) {
		this.pager = pager;
	}
	
	public int getTotal() {
		if (pager != null) {
			return pager.getTotalRows();
		}
		return 0;
	}

	public int getPage() {
		if (pager != null) {
			return pager.getCurrentPage();
		}
		return 0;
	}

	protected void getError(BizException e) {
		getError(e, "");
	}
	
	protected void getError(BizException e,String message) {
		if(e.getType().equals(Type.SERVICE)){
			this.errorMessage = e.getMessage();
		}else{
			this.errorMessage = message;
		}
		this.resultCode=SysStatic.REQUEST_CODE_ERROR;
	}
	
	public boolean isAuto() {
		return auto;
	}
	
	/**
	 * 是否使用原生字段进行查询
	 * @return
	 */
	protected boolean isNativeField() {
		return false;
	}
	
	/**
	 * 
	 * @ClassName: BaseAction.java
	 * @Description: 获取排序属性
	 * @Author:Mengfh
	 * @CreateDate:2013-6-7 下午5:39:15
	 * @return
	 */
	@JSON(serialize=false)
	public List<OrderHelper> getOrder() {
		List<OrderHelper> orderList = new ArrayList<OrderHelper>();
		sortname = this.getSortname();
		sortorder = this.getSortorder();
		if(this.getSortname() == null) {
			return null;
		}
		//拆分排序
		String[] sortNames = sortname.split(",");
		String[] sortorders = sortorder.split(",");
		for (int i=0;i<sortNames.length;i++) {
			String name = sortNames[i];
			if(StringUtils.isNotBlank(name)){
				OrderHelper order = new OrderHelper();
				//是否使用原生字段进行查询
				if(isNativeField()) {
					order.setAuto(false);
					order.setIsfetch(true);
				} else {
					order.setAuto(isAuto()); //设置自动添加前缀
					order.setIsfetch(isFetch()); //设置排序属性抓取
				}
				order.setPropertyName(name);
				if(sortorders[i].equals("asc")){
					order.setType(OrderHelper.Type.ASC);
				}else{
					order.setType(OrderHelper.Type.DESC);
				}
				orderList.add(order);
			}
		}
		//默认增加多一个按id升序排序，防止Oracle翻页会出现重复内容的问题
		OrderHelper order = new OrderHelper();
		order.setPropertyName("id");
		order.setType(OrderHelper.Type.ASC);
		orderList.add(order);
		return orderList;
	}

	public String getSortname() {
		return sortname;
	}

	public void setSortname(String sortname) {
		this.sortname = sortname;
	}

	public String getSortorder() {
		return sortorder;
	}

	public void setSortorder(String sortorder) {
		this.sortorder = sortorder;
	}

	public void setNums(int[] nums) {
		this.nums = nums;
	}

	public void setPrices(double[] prices) {
		this.prices = prices;
	}
	
	public String getUpdateURL() {
		return updateURL;
	}

	public void setUpdateURL(String updateURL) {
		this.updateURL = updateURL;
	}
	
	@JSON(serialize = false)
	public Set<Map.Entry<Object, String>> getStatusMap() {
		return this.statusMap.entrySet();
	}
	
	public String getUpdateField() {
		return updateField;
	}

	public void setUpdateField(String updateField) {
		this.updateField = updateField;
	}
	@JSON(serialize = false)
	public List<String> getCodeList() {
		return codeList;
	}

	public void setCodeList(List<String> codeList) {
		this.codeList = codeList;
	}
	@JSON(serialize = false)
	public Map<String, Integer> getCodeMap() {
		return codeMap;
	}

	public void setCodeMap(Map<String, Integer> codeMap) {
		this.codeMap = codeMap;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}
	
	/**
	 * 
	 * @ClassName: BaseAction.java
	 * @Description: 获取当前的登陆用户
	 * @Author:Mengfh
	 * @CreateDate:2013-6-7 下午5:39:38
	 * @return
	 * @throws BizException
	 */
	@JSON(serialize = false)
	protected UserInfoVo getLoginUser() throws BizException {
		try {
			UserInfoVo userInfo = (UserInfoVo) ActionContext.getContext().getSession().get(SysStatic.MC_LOGIN);
			if(userInfo == null){
				throw new Exception();
			}
			return userInfo;
		} catch (Exception e) {
			throw new BizException("获取当前操作用户信息失败!", e);
		}
	}
}